﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Interfaces;

namespace Reminder
{
    public partial class LocationDialog : Form
    {
        private ReminderControl owner;
        private Location location;

        public Location SelectedLocation
        {
            get
            {
                return location;
            }
            set
            {
                location = value;
                LoadInfo();
                LoadPositions();
            }
        }

        public LocationDialog(ReminderControl owner)
        {
            InitializeComponent();
            this.owner = owner;
        }

        private void LocationDialog_Activated(object sender, EventArgs e)
        {
            //LoadPositions();
        }

        private void LoadInfo()
        {
            txtName.Text = location.Name;
            switch (location.Type)
            {
                case LocationType.Fixed:
                    cmbType.SelectedIndex = 0;
                    break;
                case LocationType.Mobile:
                    cmbType.SelectedIndex = 1;
                    break;
            }

            LoadLocations();
            if (location.Parent != null)
            {
                //cmbParent.SelectedText = location.Parent.Name;
                cmbParent.SelectedItem = location.Parent;
            }
            else
            {
                cmbParent.SelectedIndex = 0;
            }
        }

        private void LoadLocations()
        {
            cmbParent.Items.Clear();
            cmbParent.Items.Insert(0, "None");
            foreach (Location location in owner.Locations)
            {
                if (location == this.location)
                {
                    continue;
                }

                // The graph has to be acyclic. (DAG)
                bool notAllowed = false;
                for (Location l = location.Parent; l != null; l = l.Parent)
                {
                    if (l == this.location)
                    {
                        notAllowed = true;
                        break;
                    }
                }
                if (notAllowed)
                {
                    continue;
                }

                //cmbParent.Items.Add(location.Name);
                cmbParent.Items.Add(location);
            }
        }

        private void LoadPositions()
        {
            lvwPositions.Items.Clear();

            PositionOriginator[] originators = new PositionOriginator[1];
            originators[0] = PositionOriginator.Cell;
            string[] cells = owner.Owner.Host.GetCurrentContext(originators);
            originators[0] = PositionOriginator.Wifi;
            string[] aps = owner.Owner.Host.GetCurrentContext(originators);

            AddPositions(cells, PositionOriginator.Cell);
            AddPositions(aps, PositionOriginator.Wifi);
            
            foreach (Position position in location.Positions)
            {
                bool found = false;
                foreach (ListViewItem item in lvwPositions.Items)
                {
                    if (item.Text == position.Id)
                    {
                        item.Checked = true;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    ListViewItem item = new ListViewItem(position.Id);
                    item.Checked = true;
                    item.SubItems.Add(position.Type.ToString());
                    item.Tag = position.Type;
                    lvwPositions.Items.Add(item);
                }
            }
        }

        private void AddPositions(string[] ids, PositionOriginator type)
        {
            foreach (string id in ids)
            {
                ListViewItem item = new ListViewItem(id);
                item.SubItems.Add(type.ToString());
                item.Tag = type;
                lvwPositions.Items.Add(item);
            }
        }

        private void LocationDialog_Closing(object sender, CancelEventArgs e)
        {
            if (txtName.Text == string.Empty)
            {
                MessageBox.Show("The location must have a name.", "Reminder");
                e.Cancel = true;
            }

            location.Name = txtName.Text;
            location.Type = cmbType.SelectedIndex == 1 ? LocationType.Mobile :
                LocationType.Fixed;
            location.Parent = owner.Locations.FirstOrDefault(l => l == cmbParent.SelectedItem);
            SavePositions();
        }

        private void SavePositions()
        {
            location.Positions.Clear();
            foreach (ListViewItem item in lvwPositions.Items)
            {
                if (!item.Checked)
                {
                    continue;
                }
                
                PositionOriginator type = (PositionOriginator)item.Tag;
                location.Positions.Add(new Position { Id = item.Text, Type = type });
            }
        }

        private void mnuRefresh_Click(object sender, EventArgs e)
        {
            SavePositions();
            LoadPositions();
        }
    }
}